/*
 *  Calibration.cpp
 *  portVideoBlobDetection
 *
 *  Created by Pawe? So?yga on 7/23/07.
 *  Copyright 2007 __MyCompanyName__. All rights reserved.
 *
 */

#include "Calibration.h"

Calibration::Calibration()
{
	isOn = true;
}

bool Calibration::cameraToScreen(TouchData &td)
{
	if(isOn)
	{
		if( isInRect(td))
		{
			calculateCoordinates(td);
			return true;
		}
		else
			return false;
	}
	return true;
}

void Calibration::setOn(bool isOn)
{
	this->isOn = isOn;
}

void Calibration::setMode(Calibration::MODES newMode)
{
	this->mode = newMode;
}


void Calibration::setScreenRect(TouchData luc, TouchData drc)
{
	leftUpCorner = luc;
	downRightCorner = drc;
	
	calculateProportions();
}

void Calibration::calculateProportions()
{
	proportionWidth = 1/(downRightCorner.Y - leftUpCorner.Y);
	proportionHeight = 1/(downRightCorner.X - leftUpCorner.X);
}

void Calibration::getScreenRect(TouchData &luc, TouchData &drc)
{
	luc = leftUpCorner;
	drc = downRightCorner;
}

bool Calibration::isInRect(TouchData &td)
{
	return true;
}

void Calibration::resize(Calibration::SIDES what, float howmuch)
{
	switch(what)
	{
		case Calibration::UP:
			resizeUp(howmuch);
			break;
		case Calibration::DOWN:
			resizeDown(howmuch);
			break;
		case Calibration::LEFT:
			resizeLeft(howmuch);
			break;
		case Calibration::RIGHT:
			resizeRight(howmuch);
			break;
	}
}

void Calibration::move(Calibration::SIDES where, float howmuch)
{
	switch(where)
	{
		case Calibration::UP:
			moveUp(howmuch);
			break;
		case Calibration::DOWN:
			moveDown(howmuch);
			break;
		case Calibration::LEFT:
			moveLeft(howmuch);
			break;
		case Calibration::RIGHT:
			moveRight(howmuch);
			break;
	}
}
	
bool Calibration::readConfig(std::string configFile)
{
	return true;
}

bool Calibration::saveConfig(std::string configFile)
{
	return true;
}

void Calibration::resizeUp( float amount)
{
	leftUpCorner.X -= amount;
	calculateProportions();
}

void Calibration::resizeDown( float amount)
{
	downRightCorner.X += amount;
	calculateProportions();
}

void Calibration::resizeLeft( float amount)
{
	leftUpCorner.Y -= amount;
	calculateProportions();
}

void Calibration::resizeRight( float amount)
{
	downRightCorner.Y += amount;
	calculateProportions();
}
	
void Calibration::moveUp( float amount)
{
	leftUpCorner.X -= amount;
	downRightCorner.X -= amount;
}

void Calibration::moveDown( float amount)
{
	leftUpCorner.X += amount;
	downRightCorner.X += amount;
}

void Calibration::moveLeft( float amount)
{
	leftUpCorner.Y -= amount;
	downRightCorner.Y -= amount;
}

void Calibration::moveRight( float amount)
{
	leftUpCorner.Y += amount;
	downRightCorner.Y += amount;
}
	
void Calibration::calculateCoordinates(TouchData &td)
{
	td.X = (td.X - leftUpCorner.X)*proportionHeight;
	td.Y = (td.Y - leftUpCorner.Y)*proportionWidth;
}